package com.wiz.weixin.manager;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wiz.weixin.TO.AgentWxRelationTO;
import com.wiz.weixin.mapper.AgentMapper;
import com.wiz.weixin.mapper.TencentMapper;

@Service("agentManager")
public class AgentManager {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	public AgentMapper agentMapper;
	@Resource
	public TencentMapper tencentMapper;
	
	/**
	 * 绑定账号
	 * @param wxId
	 * @param phoneNum
	 * @return
	 */
	public String bindAgentMemberByPhone(String wxId,String phoneNum){
		AgentWxRelationTO relationTo = tencentMapper.selectAgentWxRelationByWxId(wxId);
		if(relationTo == null || relationTo.getAgentId() < 1){
			return "请在下方菜单绑定微信号";
		}
		int count = agentMapper.countUnuseMemberByAgentAdminId(relationTo.getAgentId());
		if(count<1){
			return "您已经没有可绑定的账号";
		}
		count = agentMapper.countUnuseMemberByPhoneNum(phoneNum);
		if(count>0){
			return "该手机号已经绑定过账号";
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("_agentAdminId", relationTo.getAgentId());
		params.put("_phoneNum", phoneNum);
		try{
			agentMapper.insertAgentMember(params);
		}catch(Exception e){
			return "绑定失败，请联系管理员";
		}
		return "绑定成功";
	}
}
